home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJTST200.ZIP / tests / libc / ansi / math / elefunt / ttan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-01  |  6.5 KB  |  279 lines

  1. /* -*-C-*- ttan.c */
  2.  
  3. #include "elefunt.h"
  4.  
  5. /*#     program to test tan/cotan
  6. #
  7. #     data required
  8. #
  9. #        none
  10. #
  11. #     subprograms required from this package
  12. #
  13. #        machar - an environmental inquiry program providing
  14. #                 information on the floating-point arithmetic
  15. #                 system.  note that the call to machar can
  16. #                 be deleted provided the following three
  17. #                 parameters are assigned the values indicated
  18. #
  19. #                 ibeta  - the radix of the floating-point system
  20. #                 it     - the number of base-ibeta digits in the
  21. #                          significand of a floating-point number
  22. #                 minexp - the largest in magnitude negative
  23. #                          integer such that float(ibeta)**minexp
  24. #                          is a positive floating-point number
  25. #
  26. #        ran(k) - a function subprogram returning random real
  27. #                 numbers uniformly distributed over (0,1)
  28. #
  29. #
  30. #     standard fortran subprograms required
  31. #
  32. #         abs, alog, amax1, cotan, float, tan, sqrt
  33. #
  34. #
  35. #     latest revision - december 6, 1979
  36. #
  37. #     author - w. j. cody
  38. #              argonne national laboratory
  39. #
  40. # */
  41.  
  42. void
  43. ttan()
  44. {
  45.  
  46.     int i,
  47.         ibeta,
  48.         iexp,
  49.         irnd,
  50.         it,
  51.         i1,
  52.         j,
  53.         k1,
  54.         k2,
  55.         k3,
  56.         machep,
  57.         maxexp,
  58.         minexp,
  59.         n,
  60.         negep,
  61.         ngrd;
  62.  
  63.     float a,
  64.         ait,
  65.         albeta,
  66.         b,
  67.         beta,
  68.         betap,
  69.         c1,
  70.         c2,
  71.         del,
  72.         eps,
  73.         epsneg,
  74.         half,
  75.         pi,
  76.         r6,
  77.         r7,
  78.         w,
  79.         x,
  80.         xl,
  81.         xmax,
  82.         xmin,
  83.         xn,
  84.         x1,
  85.         y,
  86.         z,
  87.         zz;
  88.  
  89.     machar(&ibeta, &it, &irnd, &ngrd, &machep, &negep, &iexp, &minexp,
  90.         &maxexp, &eps, &epsneg, &xmin, &xmax);
  91.  
  92.     beta = (float) ibeta;
  93.     albeta = ALOG(beta);
  94.     half = 0.5e0L;
  95.     ait = (float) it;
  96.     pi = 3.14159265e0L;
  97.     a = ZERO;
  98.     b = pi * 0.25e0L;
  99.     n = 2000;
  100.     xn = (float) n;
  101.     i1 = 0;
  102.  
  103.     /* random argument accuracy tests */
  104.  
  105.     for (j = 1; j <= 4; ++j)
  106.     {
  107.     k1 = 0;
  108.     k3 = 0;
  109.     x1 = ZERO;
  110.     r6 = ZERO;
  111.     r7 = ZERO;
  112.     del = (b - a) / xn;
  113.     xl = a;
  114.  
  115.     for (i = 1; i <= n; ++i)
  116.     {
  117.         x = del * ran(i1) + xl;
  118.         y = x * half;
  119.         x = y + y;
  120.         if (j == 4)
  121.         {
  122.         z = cotan(x);
  123.         zz = cotan(y);
  124.         w = 1.0e0L;
  125.         if (z != ZERO)
  126.         {
  127.             w = ((half - zz) + half) * ((half + zz) + half);
  128.             w = (z + w / (zz + zz)) / z;
  129.         }
  130.         }
  131.         else
  132.         {
  133.         z = tan(x);
  134.         zz = tan(y);
  135.         w = 1.0e0L;
  136.         if (z != ZERO)
  137.         {
  138.             w = ((half - zz) + half) * ((half + zz) + half);
  139.             w = (z - (zz + zz) / w) / z;
  140.         }
  141.         }
  142.         if (w > ZERO)
  143.         k1 = k1 + 1;
  144.         if (w < ZERO)
  145.         k3 = k3 + 1;
  146.         w = ABS(w);
  147.         if (w > r6)
  148.         {
  149.         r6 = w;
  150.         x1 = x;
  151.         }
  152.         r7 = r7 + w * w;
  153.         xl = xl + del;
  154.     }
  155.  
  156.     k2 = n - k3 - k1;
  157.     r7 = sqrt(r7 / xn);
  158.     if (j != 4)
  159.         printf("\fTEST OF TAN(X) VS 2*TAN(X/2)/(1-TAN(X/2)**2)\n\n\n");
  160.     if (j == 4)
  161.         printf("\fTEST OF COT(X) VS (COT(X/2)**2-1)/(2*COT(X/2))\n\n\n");
  162.     printf("%7d RANDOM ARGUMENTS WERE TESTED FROM THE INTERVAL\n", n);
  163.     printf("    (" F15P4E "," F15P4E ")\n\n\n", a, b);
  164.     if (j != 4)
  165.     {
  166.         printf(" TAN(X) WAS LARGER%6d TIMES,\n", k1);
  167.         printf("            AGREED%6d TIMES, AND\n", k2);
  168.         printf("       WAS SMALLER%6d TIMES.\n\n", k3);
  169.     }
  170.     if (j == 4)
  171.     {
  172.         printf(" COT(X) WAS LARGER%6d TIMES,\n", k1);
  173.         printf("            AGREED%6d TIMES, AND\n", k2);
  174.         printf("       WAS SMALLER%6d TIMES.\n\n\n", k3);
  175.     }
  176.     printf(
  177. " THERE ARE%4d BASE%4d SIGNIFICANT DIGITS IN A FLOATING-POINT NUMBER\n\n\n",
  178.         it, ibeta);
  179.     w = -999.0e0;
  180.     if (r6 != ZERO)
  181.         w = ALOG(ABS(r6)) / albeta;
  182.     printf(" THE MAXIMUM RELATIVE ERROR OF" F15P4E " = %4d **" F7P2F "\n",
  183.         r6, ibeta, w);
  184.     printf("    OCCURRED FOR X =" F17P6E "\n", x1);
  185.     w = AMAX1(ait + w, ZERO);
  186.     printf(
  187.         " THE ESTIMATED LOSS OF BASE%4d SIGNIFICANT DIGITS IS" F7P2F "\n\n\n",
  188.         ibeta, w);
  189.     w = -999.0e0;
  190.     if (r7 != ZERO)
  191.         w = ALOG(ABS(r7)) / albeta;
  192.     printf(" THE ROOT MEAN SQUARE RELATIVE ERROR WAS" F15P4E " = %4d **" F7P2F "\n",
  193.         r7, ibeta, w);
  194.     w = AMAX1(ait + w, ZERO);
  195.     printf(
  196.         " THE ESTIMATED LOSS OF BASE%4d SIGNIFICANT DIGITS IS" F7P2F "\n\n\n",
  197.         ibeta, w);
  198.     if (j != 1)
  199.     {
  200.         a = pi * 6.0e0L;
  201.         b = a + pi * 0.25e0L;
  202.     }
  203.     else
  204.     {
  205.         a = pi * 0.875e0L;
  206.         b = pi * 1.125e0L;
  207.     }
  208.     }
  209.  
  210.     /* special tests */
  211.  
  212.     printf("\fSPECIAL TESTS\n\n\n");
  213.     printf(" THE IDENTITY  TAN(-X) = -TAN(X)  WILL BE TESTED.\n\n");
  214.     printf("        X         F(X) + F(-X)\n\n");
  215.  
  216.     for (i = 1; i <= 5; ++i)
  217.     {
  218.     x = ran(i1) * a;
  219.     z = tan(x) + tan(-x);
  220.     printf(F15P7E F15P7E "\n\n", x, z);
  221.     }
  222.  
  223.     printf(" THE IDENTITY TAN(X) = X , X SMALL, WILL BE TESTED.\n\n");
  224.     printf("        X         X - F(X)\n\n");
  225.     betap = ipow(beta, it);
  226.     x = ran(i1) / betap;
  227.  
  228.     for (i = 1; i <= 5; ++i)
  229.     {
  230.     z = x - tan(x);
  231.     printf(F15P7E F15P7E "\n\n", x, z);
  232.     x = x / beta;
  233.     }
  234.  
  235.     printf(" TEST OF UNDERFLOW FOR VERY SMALL ARGUMENT.\n\n");
  236.     x = pow((float) beta, (float) (minexp * 0.75e0));
  237.     y = tan(x);
  238.     printf("       TAN(" F13P6E ") =" F13P6E "\n\n", x, y);
  239.     c1 = -225.0e0L;
  240.     c2 = -.950846454195142026e0L;
  241.     x = 11.0e0L;
  242.     y = tan(x);
  243.     w = ((c1 - y) + c2) / (c1 + c2);
  244.     z = ALOG(ABS(w)) / albeta;
  245.     printf(" THE RELATIVE ERROR IN TAN(11) IS" F15P7E " = %4d **" F7P2F " WHERE\n\n",
  246.     w, ibeta, z);
  247.     printf("      TAN(" F13P6E ") =" F13P6E "\n\n", x, y);
  248.     w = AMAX1(ait + z, ZERO);
  249.     printf(
  250.     " THE ESTIMATED LOSS OF BASE%4d SIGNIFICANT DIGITS IS" F7P2F "\n\n\n",
  251.     ibeta, w);
  252.  
  253.     /* test of error returns */
  254.  
  255.     printf("\fTEST OF ERROR RETURNS\n\n\n");
  256.  
  257.     x = pow((float) beta, (float) (it / 2));
  258.     printf(" TAN WILL BE CALLED WITH THE ARGUMENT" F15P4E "\n",x);
  259.     printf(" THIS SHOULD NOT TRIGGER AN ERROR MESSAGE\n\n\n");
  260.     fflush(stdout);
  261.     errno = 0;
  262.     y = tan(x);
  263.     if (errno)
  264.     perror("tan()");
  265.     printf(" TAN RETURNED THE VALUE" F15P4E "\n\n\n\n", y);
  266.  
  267.     x = betap;
  268.     printf("\nTAN WILL BE CALLED WITH THE ARGUMENT" F15P4E "\n", x);
  269.     printf(" THIS SHOULD TRIGGER AN ERROR MESSAGE\n\n\n");
  270.     fflush(stdout);
  271.     errno = 0;
  272.     y = tan(x);
  273.     if (errno)
  274.     perror("tan()");
  275.     printf(" TAN RETURNED THE VALUE" F15P4E "\n\n\n\n", y);
  276.  
  277.     printf(" THIS CONCLUDES THE TESTS\n");
  278. }
  279.